// pages/my/vip/vip_invitation/vip_invitation_detail/index.js
const ControllerMy = require('../../../../controllers/ControllerMy').controller;
const Controller = require('../../../../controllers/Controller.js').controller;

const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210909/885563688243691520.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210914/887356889128501248.png",
    invitationId:'',
    invitationInfo:{},
    userMsg:{},
    shopDetail:{},
    receivePage:[],
    lastPage:false,
    totalRow:'',
    pageNumber:1,
    imageList:[],
    showDialog:false,
    shareData:{},
    num:'',
  },

  onLoad: function (options) {
    this.setData({
      invitationId:options.invitationId,
    })
    
  },

  onShow: function () {
    if (wx.getStorageSync('usertoken')) {
      this.getUserMsg()
      this.getInvitationDetail()
    } else {
      //无usertoken，等待login登录回调
      app.usertokenReadyCallback = res => {
        if (res.data.sessionId) {
      this.getUserMsg()
      this.getInvitationDetail()
        }
      }
    }
  },
  onReachBottom(){
    if(this.data.lastPage)return
    this.getReceivePage()
  },
  getUserMsg() {
    Controller.getAccountDetail().then(res => {
      this.setData({
        userMsg: res.data,
      })
    })
  },
  // 获取邀请详情
  getInvitationDetail(){
    let value={
      invitationId:this.data.invitationId,
      sessionId:wx.getStorageSync('usertoken'),
    }
    ControllerMy.invitationDetail(value).then(res=>{
      let shopId=wx.getStorageSync('shopId')
      let shop=res.data.shop
      // 分享页、服务通知进入,写入相关信息
      if(shop.id != shopId){
        app.globalData.shopId = shop.id
        wx.setStorageSync('shopId', shop.id)
        
        if (shop.typeName.indexOf('美业') != -1) {
          app.globalData.globalStype = 'pinStyle'
        } else {
          app.globalData.globalStype = 'yellowStyle'
        }
      }
      // 写入shopId之后获取领取列表
      this.getReceivePage(true)
      
      
      res.data.agreeTime = res.data.agreeTime.slice(0,16)
      let imageList= res.data.image
      imageList = imageList?imageList.split(','):[]
      this.setData({
        invitationInfo:res.data,
        shopDetail:res.data.shop,
        imageList
      })
    })
  },
   // 获取领取列表
   getReceivePage(isFirst = false){
    if(isFirst){
      this.setData({
        pageNumber:1,
        receivePage:[]
      })
    }
    let pageNumber=this.data.pageNumber
    let receivePage=this.data.receivePage
    let value={
      shopId:wx.getStorageSync('shopId'),
      sessionId:wx.getStorageSync('usertoken'),
      accountId:wx.getStorageSync('accountId'),
      invitationId:this.data.invitationId,
      pageNumber,
      pageSize:20
    }
    pageNumber++
    ControllerMy.receivePage(value).then(res=>{
      receivePage=receivePage.concat(res.data.list)
      this.setData({
        lastPage:res.data.lastPage,
        totalRow:res.data.totalRow,
        receivePage,
        pageNumber
      })
    })
  },
  toInvitation(){
    wx.redirectTo({
      url: '../index',
    })
  },
  previewImg(e){
    const index = e.target.dataset.index
    const imageList = this.data.imageList
    wx.previewImage({
      current:imageList[index],
      urls: imageList,
    })
  },
  openDialog(e){
    this.setData({
      showDialog:true,
    })
  },
  closeDialog(){
    this.setData({
      showDialog:false,
      num:''
    })
  },
  inputNum(e) {
    let num = e.detail.value
    const shareNum= this.data.invitationInfo.invitationAccount.num
    console.log(shareNum);
    if(num==''||(num>0&&num<=shareNum)){
      this.setData({
        num
      })
    }else{
      wx.showToast({
        title: '请正确填写分享数',
        icon: 'none',
        duration: 1500
      });
      this.setData({
        num:1
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    this.setData({
      showDialog:false,
    })

    let num = this.data.num?this.data.num:1

    let invitationId= this.data.invitationId
    let invitationAccountId= this.data.invitationInfo.invitationAccount.id
    let title = this.data.invitationInfo.invitationAccount.invitationNick+'的邀请函'
    // 私密转发
    // wx.updateShareMenu({
    //   withShareTicket: true,
    //   isPrivateMessage: true,
    //   activityId: invitationAccountId,
    // })
    return {
      title,
      path: '/pages/my/vip/vip_invitation/vip_invitation_share/index?invitationId='+invitationId+'&invitationAccountId='+invitationAccountId+'&num='+num,
      imageUrl:"/assets/my/img/invitation_4.jpg",
    }
  }
})